﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.TemplateMaker;

public partial class Manage_Cms_HtmlCreateProgress : System.Web.UI.Page
{
    private readonly string _act = PageRequest.GetString("act");
    protected int total = PageRequest.GetInt("total", -1);//-1开始，0没有
    protected int current = PageRequest.GetInt("current", 0);
    protected int SiteID = PageRequest.GetInt("siteid", -1);
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();
        switch (_act)
        {
            case "index": //首页
                //生成首页权限
                ManageHelper.CheckAdminPower("site_htmlindex_" + SiteID);
                Publish publishIndex = new Publish();
                publishIndex.PublishIndexPage(PageRequest.GetInt("siteid", -1));
                total = 1;
                current = 1;
                break;
            case "node":
                //生成栏目权限
                    ManageHelper.CheckAdminPower("site_htmlnode_" + SiteID);
                string nodeIDCollection = PageRequest.GetString("nodeids");
                total = nodeIDCollection.Split(",".ToCharArray()).Length;
                if (!string.IsNullOrEmpty(nodeIDCollection))
                {
                    int[] nodeIDs = new int[nodeIDCollection.Split(",".ToCharArray()).Length];
                    for (int i = 0; i < nodeIDCollection.Split(",".ToCharArray()).Length; i++)
                    {
                        nodeIDs[i] = Convert.ToInt32(nodeIDCollection.Split(",".ToCharArray())[i]);
                    }
                    Publish publishContent = new Publish();
                    publishContent.PublishNodePage(nodeIDs);
                    total = 1;
                    current = 1;
                }
                else
                {
                    Response.Redirect("HtmlCreate.aspx?siteid=" + SiteID +
                                      "&act=node&errormsg=请首先选中希望生成的节点！");
                    Response.End();
                }
                break;
            case "content":
                //生成内容权限
                ManageHelper.CheckAdminPower("site_htmlcontent_" + SiteID);
                string nodeIDCollection2 = PageRequest.GetString("nodeids");
                if (!string.IsNullOrEmpty(nodeIDCollection2))
                {
                    KeRui.BLL.CmsContent bllCmsContent = new KeRui.BLL.CmsContent();
                    //时间条件
                    //where publishtime between '2010-3-21 15:00:41' and '2010-3-30 15:00:41'
                    string publishtime = PageRequest.GetString("time");
                    if (publishtime != "")
                    {
                        string[] times = Utils.SplitString(publishtime, ",");
                        string time = " and publishtime between '" + times[0] + "' and '" + times[1] + "'";
                        if (total == -1)
                        {
                            total = 0;
                            foreach (string nodeID in nodeIDCollection2.Split(",".ToCharArray()))
                            {
                                int intNodeID = Convert.ToInt16(nodeID);
                                string where = "NodeID=" + intNodeID + " and IsDelete=0" + time;
                                int num_all = bllCmsContent.GetCount(where);
                                total += num_all;
                                if (total == 0)
                                {
                                    Response.Redirect("HtmlCreate.aspx?siteid=" + SiteID +
                                      "&act=content&errormsg=没有需要生成的文章！");
                                    Response.End();
                                }
                            }
                        }
                        Publish publishContent = new Publish();
                        current += publishContent.PublishContent(nodeIDCollection2, 1, time, current, 10);
                    }
                    else
                    {
                        if (total == -1)
                        {
                            total = 0;
                            foreach (string nodeID in nodeIDCollection2.Split(",".ToCharArray()))
                            {
                                int intNodeID = Convert.ToInt16(nodeID);
                                string where = "NodeID=" + intNodeID + " and IsDelete=0";
                                int num_all = bllCmsContent.GetCount(where);
                                total += num_all;
                                if (total == 0)
                                {
                                    Response.Redirect("HtmlCreate.aspx?siteid=" + SiteID +
                                      "&act=content&errormsg=没有需要生成的文章！");
                                    Response.End();
                                }
                            }
                        }
                        Publish publishContent = new Publish();
                        current += publishContent.PublishContent(nodeIDCollection2, 1, "", current, 10);
                    }
                    if (current < total && total!=0)
                    {
                        string URL = "HtmlCreateProgress.aspx?siteid=" + SiteID + "&nodeids=" + nodeIDCollection2 + "&act=content&total=" + total + "&current=" + current + "&time=" + publishtime + ";";
                        Response.Write("<meta http-equiv='refresh' content='1;url="+URL+"'> ");
                    }
                }
                else
                {
                    Response.Redirect("HtmlCreate.aspx?siteid=" + SiteID +
                                      "&act=content&errormsg=请首先选中希望生成内容页面的栏目！");
                    Response.End();
                }
                break;
        }
    }
}
